package com.yanqu.road.connect;

import com.yanqu.road.message.YanQuMessage;
import io.netty.channel.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.net.InetSocketAddress;

public class ServerConnectionGroup extends ConnectionGroup<YanQuConnection> {

    private static Logger logger = LogManager.getLogger(ServerConnectionGroup.class.getName());

    protected String ipAddress;
    protected String serverName;
    protected long serverId;
    protected int serverIndex;
    protected int port;
    protected boolean isActive;
    protected int serverType;


    public ServerConnectionGroup(int size) {
        super(size);
    }

    @Override
    protected YanQuConnection connect(int index) throws Exception {
        return null;
    }

    public ServerConnectionGroup(String ipAddress, String serverName, int size, int serverIndex, int port, int serverType) {
        super(size);
        this.ipAddress = ipAddress;
        this.serverName = serverName;

        this.serverIndex = serverIndex;
        this.port = port;
        this.serverType = serverType;
        this.isActive = false;

    }


    protected YanQuConnection createConnection(Channel channel, int index, InetSocketAddress addr) {
        return null;
    }

    public void setServerId(long serverId) {
        this.serverId = serverId;
    }


    public String getIpAddress() {
        return ipAddress;
    }

    public int getPort() {
        return port;
    }


    public int getServerType() {
        return serverType;
    }

    public long getServerId() {
        return serverId;
    }


    public String getServerName() {
        return serverName;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }


    public void send(YanQuMessage packet) {
        write(packet.getPlayerId(), packet);
    }


    public void sendPacket(long userId, YanQuMessage packet) {
        write(userId, packet);
    }

    public void addConnection(Connection connection, int index) {

        // 合法性校验
        if (index >= allConnections.length) {
            logger.error("索引越界. current: {}, index:{}", allConnections.length, index);
            return;
        }
        allConnections[index] = connection;
        logger.info("新增连接. 索引编号index: {}", index);
    }


}
